//阻塞队列有四组API
//下列是对这四组API的测试
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class juc_4API {
    public static void main(String[] args) throws InterruptedException {
       // test1();
       // test2();
       // test3();
        test4();

    }

    private static void test4() throws InterruptedException {
        //添加元素： offer( , , )
        //移除元素： poll( , )
        //检测队首元素： 无
        //使用这几组API，如果超出capacity或队列为空时等待一定的时间，超出设置的等待时间时停止
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue(3);
        linkedBlockingQueue.offer("a",2, TimeUnit.SECONDS);
        linkedBlockingQueue.offer("b",2, TimeUnit.SECONDS);
        linkedBlockingQueue.offer("c",2, TimeUnit.SECONDS);
        linkedBlockingQueue.offer("d",2, TimeUnit.SECONDS);

        linkedBlockingQueue.poll(2,TimeUnit.SECONDS);
        linkedBlockingQueue.poll(2,TimeUnit.SECONDS);
        linkedBlockingQueue.poll(2,TimeUnit.SECONDS);
        linkedBlockingQueue.poll(2,TimeUnit.SECONDS);

    }

    private static void test3() throws InterruptedException {
        //添加元素： put()
        //移除元素： take()
        //检测队首元素： 无
        //使用这几组API，超出capacity或为空时阻塞等待
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue(3);
        linkedBlockingQueue.put("a");
        linkedBlockingQueue.put("b");
        linkedBlockingQueue.put("c");
        linkedBlockingQueue.put("d");

        linkedBlockingQueue.take();
        linkedBlockingQueue.take();
        linkedBlockingQueue.take();
        linkedBlockingQueue.take();
    }

    private static void test2() {
        //添加元素： offer()
        //移除元素： poll()
        //检测队首元素： peek()
        //使用这几组API，有返回值，不抛出异常

        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue(3);
        linkedBlockingQueue.offer("a");
        linkedBlockingQueue.offer("b");
        linkedBlockingQueue.offer("c");
        linkedBlockingQueue.offer("d");  //不报异常，但也无法将元素加入队列
        System.out.println(linkedBlockingQueue.peek());
        linkedBlockingQueue.poll();
        System.out.println(linkedBlockingQueue.peek());
        linkedBlockingQueue.poll();
        System.out.println(linkedBlockingQueue.peek());
        linkedBlockingQueue.poll();
        System.out.println(linkedBlockingQueue.peek());   //不报异常，返回null
        linkedBlockingQueue.poll();                       //不报异常
    }

    private static void test1() {

        //添加元素： add()
        //移除元素： remove()
        //检测队首元素： element()
        //使用这几组API，会抛出异常！！！

        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue(3);
        linkedBlockingQueue.add("a");
        linkedBlockingQueue.add("b");
        linkedBlockingQueue.add("c");
     //   linkedBlockingQueue.add("d");  // 会报异常 -- IllegalStateException
        System.out.println(linkedBlockingQueue.element());
        linkedBlockingQueue.remove();
        System.out.println(linkedBlockingQueue.element());
        linkedBlockingQueue.remove();
        System.out.println(linkedBlockingQueue.element());
        linkedBlockingQueue.remove();
        linkedBlockingQueue.remove();                // //会报异常 -- NoSuchElementException
        System.out.println(linkedBlockingQueue.element());   //会报异常 -- NoSuchElementException
    }

}
